package oop.controller.action.history;

import java.util.List;

import oop.controller.action.DefaultAction;
import oop.data.History;
import oop.db.dao.HistoryDAO;
import oop.taglib.UtilFunctions;

import org.apache.commons.lang.StringUtils;

public class ListAction extends DefaultAction {

	public static final int PAGE_LENGTH = 30;

	@Override
	public void performImpl() throws Exception {
		String pageStr = StringUtils.defaultIfEmpty(request
				.getParameter("page"), "1");
		int page = Integer.parseInt(pageStr);

		List<History> histories = HistoryDAO.fetchByUser(getUser().getId(), (page - 1)
				* PAGE_LENGTH, PAGE_LENGTH);
		int count = HistoryDAO.countByUser(getUser().getId());
		title("Lịch sử các đề đã làm của "+getUser().getName());
		request.setAttribute("histories", histories);
		request.setAttribute("page", page);
		request.setAttribute("pageCount", UtilFunctions.ceil(count / (double)PAGE_LENGTH));
	}

}
